/*
 * author zby
 * 框架介绍说明
 */

(function (document, window, $, Utils) {
    // 'use strict';
    var vm = {};
    var params = Utils.getUrlParams();
    var id = 0;
    $(function () {
        var fns = {

            /**
             * 单击小手
             * @param me
             */
            evaluate: function (me) {
                let hasClass = $(me).hasClass("hand-color");
                if (hasClass) {
                    layer.msg("您已经评价过了，请勿反复评价", {time: 1000, icon: 2});
                } else {
                    if ($(".hand-color")) {
                        $(".hand-color").each(function () {
                            $(this).removeClass("hand-color");
                        })
                    }
                    $(me).addClass("hand-color");
                    const ctype = $(me).attr("ctype");
                    const cid = $(me).attr("cid");
                    if (cid && ctype) {
                        Utils.getSimpleJson({
                            url: "/front/evaluate/save",
                            params: {
                                resultEnum: ctype,
                                articleId: cid
                            },
                            fn: function () {
                                layer.msg("评价成功，谢谢你的评价", {time: 800, icon: 1});
                            }
                        })
                    }
                }
            },
            /**
             * 当单击左面的列表，会向数据库中拿值，然后填充到右面的值中
             * @param me
             */
            clickToRight: function (cid) {
                $("#evaluate").find("i").each(function () {
                    $(this).removeClass("hand-color");
                });
                vm.articleId = cid || typeof cid != 'undefined' ? cid : vm.articleId;
                //获取文章内容
                Utils.getSimpleJson({
                    url: "/front/articleCatalog/" + vm.articleId,
                    fn: function (data) {
                        let content = data.content;
                        data.content=content.replace(/<img/g,"<img data-action='zoom'");
                        vm.article = data;
                    }
                })
                //保存文章点击量
                Utils.getSimpleJson({
                    url: "/front/browserArticle/save",
                    params: {
                        articleId: vm.articleId
                    },
                    fn: function () {
                    }
                })
                //获取当前文章的点击量
                Utils.getSimpleJson({
                    url: "/front/browserArticle/countClick",
                    params: {
                        articleId: vm.articleId
                    },
                    fn: function (data) {
                        // vm.clicks = data
                    }
                })
                //获取评论
                Utils.getSimpleJson({
                    url: "/front/comment/list/",
                    params: {
                        articleId: vm.articleId
                    },
                    fn: function (data) {
                        vm.comments = data.resultData;
                    }
                })
            },
            /**
             * 发表评论
             * @param me
             */
            submitComment: function () {
                const content = $('#commentContainer').summernote('code');
                if (!content) {
                    layer.msg("评论内容不能为空", {time: 800, icon: 2});
                    return;
                }
                Utils.getSimpleJson({
                    url: id > 0 ? "/front/comment/edit/" + id : "/front/comment/save",
                    params: {
                        "content": content,
                        "commentType": $("#commentType").find(":selected").val(),
                        "articleId": vm.articleId
                    },
                    fn: function (data) {
                        vm.comments = data.resultData;
                        id = 0;
                        $("#commentContainer").summernote("code", "");
                        $("#commentContainer").summernote({
                            placeholder: '请输入评论信息...', //占位符
                        });
                    }
                })
            },
            edit: function (cid) {
                Utils.getSimpleJson({
                    url: "/front/comment/" + cid,
                    fn: function (data) {
                        vm.comment = data;
                        console.log(vm.comment)
                        id = vm.comment.id;
                        $("#commentContainer").summernote("code", vm.comment.content);
                    }
                })
            },
            del: function (cid) {
                layer.confirm("确认删除", {
                    btn: ['确认', '取消']
                }, function (index) {
                    Utils.getSimpleJson({
                        url: "/front/comment/delete",
                        params: {
                            "id": cid,
                        },
                        fn: function (data) {
                            vm.comments = data.resultData;
                        }
                    })
                    layer.close(index)
                }, function (index) {
                    layer.close(index)
                })
            },
            // /**
            //  * 下载文件
            //  */
            // download: function (cid) {
            //     var obj = {
            //         // id: cid ? cid : '',
            //         fileName: 'beantools.jar'
            //     }
            //     window.open(constant.server_path + "/core/attach/download?" + Utils.formatParams(obj));
            // },
        };
        window.fns = fns;
        init();
    });

    //入口方法
    function init() {
        vm = new Vue({
            el: '.container-fiuled',
            data: {
                articles: "",
                enums: "",
                comments: "",
                comment: "",
                article: "",
                articleId: 1,
                clicks: 0,
                attach: ""
            },
            watch: {
                // 计算属性的 getter
                comments: function () {

                },
                clicks: function () {

                }
            }
        })

        fns.clickToRight(vm.articleId);
        Utils.getSimpleJson({
            url: "/front/articleCatalog/withLevel",
            fn: function (data) {
                vm.articles = data;
            }
        })
        Utils.getSimpleJson({
            url: "/core/enum/list",
            params: {
                path: "CommentTypeEnum"
            },
            fn: function (data) {
                vm.enums = data
            }
        })
        let headers = Utils.getHeaders();
        if (headers && headers.name) {
            $("#loginLi").css("display", "none");
            $("#signInLi").css("display", "none");
            $("#userInfo").css("display", "block");
            $("#userInfo").find("span").html("欢迎：" + headers.name);
            $("#logOutLi").css("display", "block");
        }
        /**
         * 初始化summernote
         */
        $("#commentContainer").summernote({
            height: 300,
            focus: true,
            lang: 'zh-CN', //设置中文，需引入中文插件summernote-zh-CN.js
            placeholder: '请输入评论信息...', //占位符
            toolbar: [
                ['fontname', ['fontname']],
                ['style', ['bold', 'italic', 'underline', 'clear']],
                ['font', ['strikethrough', 'superscript', 'subscript']], //字体划线、字体上标、字体下标
                ['fontsize', ['fontsize']], //字体大小
                ['color', ['color']], //字体颜色
                ['style', ['style']],//样式
                ['para', ['ul', 'ol', 'paragraph']], //无序列表、有序列表、段落对齐方式
                ['height', ['height']], //行高
                ['table', ['table']], //插入表格
                ['hr', ['hr']],//插入水平线
                ['link', ['link']], //插入链接
                ['fullscreen', ['fullscreen']], //全屏
                ['codeview', ['codeview']], //查看html代码
                ['undo', ['undo']], //撤销
                ['redo', ['redo']], //取消撤销
                ['help', ['help']], //帮助
            ],
        });

        AMap.plugin('AMap.CitySearch', function () {
            var citySearch = new AMap.CitySearch()
            citySearch.getLocalCity(function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    areaId = result.adcode;
                    Utils.getSimpleJson({
                        url: "/core/systemRecord/save",
                        noAsync: true,
                        params: {
                            areaId: areaId
                        },
                        fn: function (data) {
                            $("#pageView").html("今日浏览量：" + data.todayPageView + " 总浏览量: " + data.allPageView);
                        }
                    })
                }
            })
        })
    }
})
(document, window, jQuery, Utils);